package cn.pping.user.service.impl;

import cn.pping.basic.constant.BaseConstants;
import cn.pping.basic.exception.BusinessExeption;
import cn.pping.basic.util.MD5Utils;
import cn.pping.basic.util.StrUtils;
import cn.pping.user.domain.Logininfo;
import cn.pping.user.domain.User;
import cn.pping.user.dto.RegisterDto;
import cn.pping.user.mapper.LogininfoMapper;
import cn.pping.user.mapper.UserMapper;
import cn.pping.user.service.IUserService;
import cn.pping.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author pping
 * @since 2022-07-28
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * @Title: phoneRegister 注册业务
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/29 19:13
     * @Parameters: [dto]
     * @Return void
     */
    @Override
    public void phoneRegister(RegisterDto dto) {
        // 先把dto中需要的字段拿出来
        String phone = dto.getPhone();
        String verifyCode = dto.getVerifyCode();
        String password = dto.getPassword();
        String comfirmPassword = dto.getComfirmPassword();
        // 非空校验
        if(StringUtils.isEmpty(phone)
                ||StringUtils.isEmpty(verifyCode)
                ||StringUtils.isEmpty(password)
                ||StringUtils.isEmpty(comfirmPassword)){
            throw new BusinessExeption("参数不能为空！");
        }
        // 注册业务员判断
        // 判断密码是否正确
        if (!password.equals(comfirmPassword)){
            throw new BusinessExeption("密码不一致请重新输入");
        }
        // 判断手机号是否注册过
        User byPhone = userMapper.findByPhone(phone);
        if (byPhone!=null){
            throw new BusinessExeption("该手机号已经注册");
        }
        // 判断手机验证码是否失效
        Object obj = redisTemplate.opsForValue().get(phone + BaseConstants.VerifyCodeConstan.PHONEREGISTER);
        if(obj==null){
            throw new BusinessExeption("手机验证码过期，请重新获取");
        }
        // 判断手机验证码是否一致 存入前端时通过KEY VALIUE的形式 obj是验证码+时间戳
        String redisCode = obj.toString().split(":")[0];
        if (!redisCode.equals(verifyCode)){
            throw new BusinessExeption("手机验证码输入错误，请重新输入");
        }
        // 验证到这步说明所有业务都已经处理完毕了
        // 首先保存创建对象并保存到login info
        Logininfo logininfo =dto2Logininfo(dto);
        User user =logininfo2User(logininfo);
        // 先保存logininfol
        logininfoMapper.add(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.add(user);

    }


    /**
    * @Title: Ldto2logininfo 将生成的对象保存进 登录信息页面
    * @Description: TODO
    * @Author: Pan
    * @Version: 1.0
    * @Date:  \ 19:43
    * @Parameters: [dto]
    * @Return cn.pping.user.domain.Logininfo
    */

    private Logininfo dto2Logininfo(RegisterDto dto) {
        Logininfo logininfo = new Logininfo();

        logininfo.setUsername(dto.getPhone());
        logininfo.setPhone(dto.getPhone());
        logininfo.setType(1);
        // 生成盐值 32
        String salt = StrUtils.getComplexRandomString(32);
        // 通过和密码进行加密   加盐加密：
        String dbPassword = MD5Utils.encrypByMd5(salt + dto.getPassword());
        logininfo.setSalt(salt);
        logininfo.setPassword(dbPassword);
        return logininfo;
    }

    /**
    * @Title: logininfo2user 把登录信息保存进User
    * @Description: TODO
    * @Author: Pan
    * @Version: 1.0
    * @Date:  2022/7/29 19:44
    * @Parameters: [logininfo]
    * @Return cn.pping.user.domain.User
    */

    private User logininfo2User(Logininfo logininfo) {
        User user = new User();
        // copy相同的属性
        BeanUtils.copyProperties(logininfo,user);

        return  user;
    }
}
